package com.xxxx.server.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xxxx.server.mapper.EmployeeMapper;
import com.xxxx.server.model.EmpMsg;
import com.xxxx.server.pojo.Employee;
import com.xxxx.server.query.EmpQuery;
import com.xxxx.server.service.IEmployeeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhoubin
 * @since 2020-09-07
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements IEmployeeService {

    @Resource
    private EmployeeMapper employeeMapper;

    @Override
    public List<Employee> getAllEmpInfoWithDeptName(Integer currentPage, Integer size) {
        //当前页的起始索引
        Integer pageStart = (currentPage-1)*size;
        List<Employee> list=employeeMapper.selectAllEmpInfoWithDeptName(pageStart,size);
        return list;
    }

    @Override
    public Map<String, Object> selectEmpMaxWorkId() {
        Map<String, Object> map = new HashMap<>();
        String sworkId = employeeMapper.selectEmpMaxWorkId();
        Integer workId = Integer.valueOf(sworkId) + 1;
        String ssworkId = workId.toString();
        for (int i = 0;i < sworkId.length()-workId.toString().length();i++){
            ssworkId = "0" + ssworkId;
        }
        map.put("obj",ssworkId);
        return map;
    }

    @Override
    public int insertEmp(Employee employee) {
        return employeeMapper.insert(employee);
    }

    @Override
    public Integer updateEmp(Employee employee) {
        return employeeMapper.updateById(employee);
    }

    @Override
    public Integer deleteEmp(Integer id) {
        return employeeMapper.deleteById(id);
    }

    @Override
    public Map<String, Object> selectEmpByParams(EmpQuery query, HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();

        //查询的时候如果传name过来了，那么将CurrentPage置为1，从第一页开始显示
        if (null != query.getName() && !"".equals(query.getName())){
            query.setCurrentPage(1);
        }

        if (null != query.getBeginDateScope() && !"".equals(query.getBeginDateScope())){
            query.setBeginDate(query.getBeginDateScope().split(",")[0]);
            query.setEndDate(query.getBeginDateScope().split(",")[1]);
        }

        // 开启分页
        PageHelper.startPage(query.getCurrentPage(),query.getSize());
        // 通过条件查询员工列表
        List<EmpMsg> list = employeeMapper.selectEmpByParams(query);
        // 得到分页数据
        PageInfo<EmpMsg> pageInfo = new PageInfo<>(list);

        map.put("code",0);
        map.put("msg","");
        map.put("count",list.size()); // 总记录数
        map.put("data",pageInfo.getList()); // 当前页显示的数据列表
        map.put("total",pageInfo.getTotal());
        return map;
    }
}
